﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Twittit.Web
{
    public partial class Profile : System.Web.UI.Page
    {
        static bool init = true;
        protected void Page_Load(object sender, EventArgs e)
        {
            Twittit.DataAccess.User user = new DataAccess.User();
            Twittit.Business.UserService t = Twittit.Business.UserService.getInstance();
            if (Request.QueryString["user"] == null && Session["username"] == null)
                this.Response.Redirect("Login.aspx");


            if(Session ["username"] != null && Request.QueryString["user"] == null)
                user = t.getUserByUsername(Session["username"].ToString());
            else
            {
                ModifyProfileButton.Visible = false;
                int userRequestedId = Convert.ToInt32(Request.QueryString["user"].ToString());
                user = t.getUserByID(userRequestedId);
            }   
          
           
            UserTwittsRepeater.DataSource = t.getUserTwitts(user.id);
            UserTwittsRepeater.DataBind();

            UserGravatar.Email = user.email;

            String userName = user.name;
            String userTwittit = user.twittit;
            String userMail = user.email;
            String userWebsite = user.website;
            String userLocalisation = user.localisation;
            String userAbout = user.aboutyou;
            DateTime inDate = user.inDate;
            byte userConfirmSub = user.confirmSubscription;
            String userConfirm = "Public";

            if (Request.HttpMethod == "GET")
            {
                PrivacyCheckBox.Checked = user.confirmSubscription == 1 ? true : false;
                TwitterConnectionCheckbox.Checked = user.connectToTwitter == 0 ? false : true;
                init = false;
            }

            if (userConfirmSub == 1)
            {
                userConfirm = "Privé";
            }

            if (user.connectToTwitter == 1)
            {
                TwitterConnectionLabel.Text = "Oui";
            }
            else
            {
                TwitterConnectionLabel.Text = "Non";
            }

            NameLabel.Text = userName;
            MailLabel.Text = userMail;
            TwittitLabel.Text = userTwittit;
            WebsiteLabel.Text = userWebsite;
            LocalisationLabel.Text = userLocalisation;
            AboutLabel.Text = userAbout;
            UserConfirmLabel.Text = userConfirm;
            inDateLabel.Text = inDate.ToString("dd/MM/yyyy");

            UserIDLabel.Text = user.id.ToString();

            literalUsername1.Text = userTwittit;
            literalUsername2.Text = userTwittit;

            TitleLabel.Text = userTwittit;
            SubscribedCount.Text = t.countSubscribed(user.id).ToString();
            SubscribersCount.Text = t.countSubscribers(user.id).ToString();

            userIdFollow.Text = user.id.ToString();

            if(Session["username"] != null)
            {
                Twittit.DataAccess.User current = Twittit.Business.UserService.getInstance().getUserByUsername(Session["username"].ToString());

                if (Twittit.Business.UserService.getInstance().hasSubscribed(current.id, user.id))
                {
                    SubscriptionLiteral.Text = "Se désabonner";
                    if (Twittit.Business.UserService.getInstance().hasRequested(current.id, user.id))
                    {
                        SubscriptionStatusLiteral.Text = "(En attente de confirmation)";
                    }
                }
                else
                    SubscriptionLiteral.Text = "S'abonner";
            }

        }


        protected void SubmitProfileButton_Click(object sender, EventArgs e)
        {
            int userid = Convert.ToInt16(UserIDLabel.Text);
            Twittit.DataAccess.User u = Twittit.Business.UserService.getInstance().getUserByID(userid);

            String name = NameTextBox.Text;
            String twittit = TwittitTextBox.Text;
            String mail = MailTextBox.Text;
            String website = WebsiteTextBox.Text;
            String localisation = LocalisationTextBox.Text;
            String about = AboutTextBox.Text;

            byte userConfirm = 0;
            if (PrivacyCheckBox.Checked)
            {
                userConfirm = 1;
            }

            if (this.ChekTwittit(null, twittit) || twittit == u.twittit)
            {
                Twittit.Business.UserService.getInstance().editUser(u, name, twittit, mail, website, localisation, about,userConfirm, TwitterConnectionCheckbox.Checked);                
                Session["username"] = twittit;
                Response.Redirect("Profile.aspx");
            }
        }

        private bool ChekTwittit(Object source, String value)
        {
            bool r = false;
            r = Twittit.Business.UserService.getInstance().checkTwittit(value);
            return r;
        }
    }
}